package com.ml.mall.service.company.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.company.CompanyShippingDto;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.company.SysCompanyShipping;
import com.ml.mall.mapper.company.SysCompanyShippingMapper;
import com.ml.mall.service.company.SysCompanyShippingService;
import com.ml.mall.utils.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysCompanyShippingServiceImpl extends ServiceImpl<SysCompanyShippingMapper, SysCompanyShipping> implements SysCompanyShippingService {

    @Autowired
    private SysCompanyShippingMapper sysCompanyShippingMapper;

    /**
     * 判断配送信息是否完整
     * @param companyShipping
     * @return
     */
    @Override
    public boolean isCompletedShippingInfo(SysCompanyShipping companyShipping) {
        if(null == companyShipping){
            return false;
        }
        String statesArrStr = companyShipping.getStatesArr();
        if(StrUtil.isBlank(statesArrStr)){
            return false;
        }
        List<String> statesArr = StrUtils.getListByStr(statesArrStr);
        if(CommonConsts.FIXED_NUM_ZERO.equals(statesArr.size())){
            return false;
        }
        return true;
    }

    @Override
    public void saveSupplierShipping(CompanyShippingDto companyShippingDto, Long companyId) {
        //修改配送州属信息
        if(null != companyShippingDto){
            SysCompanyShipping sysCompanyShipping = new SysCompanyShipping();
            BeanUtils.copyProperties(companyShippingDto, sysCompanyShipping);
            if(null != companyShippingDto.getStatesArr()){
                sysCompanyShipping.setStatesArr(companyShippingDto.getStatesArr().toString());
            }
            sysCompanyShipping.setCompanyId(companyId);
            sysCompanyShipping.setState(CommonConsts.FIXED_NUM_ZERO);
            sysCompanyShippingMapper.insert(sysCompanyShipping);
        }
    }
}
